package com.arcvideo.acsconnect.widget;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.arcvideo.tyingyitonginhouse.R;
import com.arcvideo.acsconnect.listener.OnConfirmListener;
import com.framework.core.utils.ToastUtil;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author shenmengchao
 * @version 1.0.0
 * @date 2017/7/28
 * @description
 */

public class InputSessionIdDialog extends Dialog {

    @BindView(R.id.et_address)
    EditText mEtAddress;
    @BindView(R.id.tv_cancel)
    TextView mTvCancel;
    @BindView(R.id.tv_confirm)
    TextView mTvConfirm;
    private Context mContext;
    private OnConfirmListener mOnConfirmListener;
    private String mSessionId;


    public InputSessionIdDialog(@NonNull Context context) {
        super(context, R.style.Dialog);
        this.mContext = context;
        init();
    }

    public void setListener(OnConfirmListener listener) {
        this.mOnConfirmListener = listener;
    }

    private void init() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_dialog_input_session_id, null, false);
        ViewGroup.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        ButterKnife.bind(this, view);
        addContentView(view, layoutParams);

        Window window = this.getWindow();
        WindowManager m = window.getWindowManager();
        Display d = m.getDefaultDisplay();
        WindowManager.LayoutParams p = window.getAttributes();
        p.width = (int) (d.getWidth() * 0.97);
//        p.height = (int) (dFormat.getHeight() * 0.7);
        window.setAttributes(p);
    }


    @Override
    public void show() {
        super.show();
    }

    private void showKeyboard(final EditText editText) {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {
                InputMethodManager inputManager = (InputMethodManager) editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.showSoftInput(editText, 0);
            }
        }, 200);
    }

    @OnClick({R.id.tv_confirm, R.id.tv_cancel})
    public void onClick(View view) {
        switch(view.getId()){
            case R.id.tv_confirm:
                if(check()){
                    if(null != mOnConfirmListener){
                        dismiss();
                        mOnConfirmListener.onConfirm(mSessionId);
                    }
                }
                break;
            case R.id.tv_cancel:
                dismiss();
                break;
            default :
                break;
        }
    }

    private boolean check(){
        mSessionId = mEtAddress.getText().toString();
        if(TextUtils.isEmpty(mSessionId)){
            ToastUtil.showToast("请输入房间号");
            return false;
        }
        return true;
    }
}
